package com.wangcang.widgetlibrary.list;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.wangcang.widgetlibrary.R;
import com.wangcang.widgetlibrary.bean.PicBean;

import java.util.ArrayList;
import java.util.List;

/**
 * 列表E样式
 * Created by ShineF on 2017/6/22 0022.
 */

public class EListActivity extends AppCompatActivity implements View.OnClickListener {

    private RecyclerView mRvPicList;
    private ImageView mIvBack;
    private List<PicBean> mList;


    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_list_e);
        init();
    }

    public void init() {
        mRvPicList = (RecyclerView) findViewById(R.id.rv_pic_list);
        mIvBack = (ImageView) findViewById(R.id.iv_back);
        mIvBack.setOnClickListener(this);
        initData();
    }

    public void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            PicBean bean = new PicBean();
            bean.setPicResc(R.drawable.ic_launcher);
            bean.setName("图片描述" + i);
            mList.add(bean);
        }
        MyAdapter adapter = new MyAdapter();
        mRvPicList.setLayoutManager(new GridLayoutManager(this, 3));
        mRvPicList.setAdapter(adapter);
    }

    @Override
    public void onClick(View v) {
        finish();
    }

    class MyHolder extends RecyclerView.ViewHolder {

        private ImageView mIvImg;
        private TextView mImgName;

        MyHolder(View itemView) {
            super(itemView);
            mIvImg = (ImageView) itemView.findViewById(R.id.iv_img);
            mImgName = (TextView) itemView.findViewById(R.id.tv_img_name);
        }
    }

    private class MyAdapter extends RecyclerView.Adapter<MyHolder> {

        @Override
        public MyHolder onCreateViewHolder(ViewGroup parent, int viewType) {
            return new MyHolder(View.inflate(EListActivity.this, R.layout.view_list_e_layout, null));
        }

        @Override
        public void onBindViewHolder(MyHolder holder, int position) {
            PicBean bean = mList.get(position);
            holder.mIvImg.setImageResource(bean.getPicResc());
            holder.mImgName.setText(bean.getName());
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }
    }
}
